import publicForm from "@/components/public/el-forms.vue"; //弹框form
import formJs from "./regexDetail.js"; //新增弹框配置项

// 
import {
    saveInstrumentInfo, //保存
    getInstrumentInfo, //详情
    getFunctionRoomList

} from "@/api/BackStage/materialQualityPage/request.js";
export default {
    props: [
        "dialogVisible",
        "dialogTitle",
        "statusNum",
        "currentRow",
        "fCompanyid"

    ],
    data() {
        return {
            visible: "",
            loading: false,
            title: null,
            isLock: false,
            dialogHeight: null,
            treeData: [], //材料种类
            formData: {
                fId: "", //主键id，有:编辑 无:新增
                companyId: "", //标段id
                companyName: "", //标段名称
                deviceId: "", //设备id
                deviceName: "", //设备名称
                specification: "", //规格型号
                manufacturer: "", //生产厂家
                produceDates: this.common.day(0), //出厂日期
                produceId: "", //出厂编号
                range: "", //量程
                technicalSpecification: "", //技术参数
                acquisitionDates: this.common.day(0), //购置日期
                measuringRange: "", //测量范围
                accuracy: "", //准确度
                calibratingPeriod: "", //检定/校准周期(月)
                manager: "", //管理人
                unit: "", //计量单位
                department: "", //所属部门
                storagePlace: "", //存储地点
                count: "", //数量
                price: "", //单价
                functionName: "", //功能室
                supplier: "", //供货商
                status: "", //使用状态
                remark: "", //备注
                picture: "", //图片
                recordfiles: [ //编辑功能需要需要删除图片信息

                ]
            },
            formObj: formJs.formObj,

            btnShow: true,
            currentDetail: null,
            fileList: [],
            userInfo:{}
        };
    },
    components: {
        publicForm,
    },
    watch: {
        dialogVisible: {
            handler(newVal) {
                if (newVal) {
                    this.visible = newVal;
                }
            },
            immediate: true,
        },
        dialogTitle: {
            handler(newVal) {
                if (newVal) {
                    this.title = newVal;
                }
            },
            immediate: true,
        },

        statusNum: {
            handler(newVal) {
                if (newVal) {

                    this.edit(newVal);
                }
            },
            immediate: true,
        },
        currentRow: {
            handler(newVal) {
                if (newVal) {
                    this.currentDetail = newVal;
                    if (this.statusNum != 1) {
                        this.getDetail({
                            fId: this.currentDetail.fId
                        })
                    }
                }
            },
            immediate: true,
        },
    },
    created() {
        window.addEventListener("resize", this.getHeight);
        this.getHeight();
    }, //获取页面高度自适应：
    destroyed() {
        window.removeEventListener("resize", this.getHeight);
        this.getHeight();
    },
    mounted() {
        this.userInfo = JSON.parse(sessionStorage.getItem("yjSmart-userInfo"));
        this.btnShow = true
        this.getFunctionRoom();

        if (this.statusNum == 1) { //新增
            // this.formData = this.$options.data.call(this).formData;
            this.btnShow = true
            this.formObj.map((item) => {
                item.disabled = false;
            });
            // this.formBcObj.map((item) => {
            //     item.disabled = false;
            // });
            this.btnShow = true
            this.formData.picture = "picture" + Date.now();
        this.formObj[21].uploadSelect=true;

            this.formData.companyId = this.userInfo.companyId; //标段id
            this.formData.companyName = this.userInfo.companyFullName; //标段名称
        } else if (this.statusNum == 2) { //编辑
            this.formData.fId = this.currentDetail.fId //按钮 
            // this.getDetail({
            //     fId: this.currentDetail.fId
            // })
            this.formObj.map((item) => {
                item.disabled = false;
            });
            // this.formBcObj.map((item) => {
            //     item.disabled = false;
            // });
            this.btnShow = true
        this.formObj[21].uploadSelect=true;

        } else if (this.statusNum == 7) { //查看
            this.btnShow = false
            this.formObj.map((item) => {
                item.disabled = true;
            });
            this.formObj[21].uploadSelect=false;
            // this.formBcObj.map((item) => {
            //     item.disabled = true;
            // });
        }
        this.getHeight();
    },
    methods: {
        getHeight() {
            var h = document.documentElement.clientHeight; //获取页面可见高度
            this.dialogHeight = h * 0.76 + "px";
        },
        handleSelect(key) {
            this.activeIndex = key;
        },
        closeDialog() {
            this.$emit("close");
        },
        selectChange(data, a, b) {
            console.log(data, a, b);
        },
        edit() {
            this.formData = this.$options.data.call(this).formData;
        },
        // 获取列表数据
        getFunctionRoom() {
            this.userInfo = JSON.parse(sessionStorage.getItem("yjSmart-userInfo"));

            var obj = {
                fCompanyid: this.userInfo.companyId, //左侧选中组织机构id
                page: 1, //当前页码
                typ: "",
                pageSize: 500, //每页显示条数

                keyWord: "", //上方查询条件样品名称
            }
            this.loading = true;
            var that = this;
            getFunctionRoomList(obj)
                .then((res) => {
                    if (res.errcode == 0) {
                        that.loading = false;
                        this.formObj[18].options = [{
                            label: "请选择",
                            value: ""
                        }]
                        var dt = this.uniqueBy(res.data.pageInfo.list, 'functionRoom')
                        dt.map(item => {
                            this.formObj[18].options.push({
                                label: item.functionRoom,
                                value: item.functionRoom
                            })
                        })
                    } else {
                        this.$message.error("列表数据获取失败", res.msg);
                        this.loading = false;
                    }
                })
               
        },
        uniqueBy(array, key) {
            const seen = new Set();
            return array.filter((item) => {
              const keyValue = item[key];
              return seen.has(keyValue) ? false : seen.add(keyValue);
            });
          },
        // 刪除文件
        delteFile(file) {
            for (let index = 0; index < this.fileList.length; index++) {
                if (file.uid == this.fileList[index].uid) {
                    if (this.fileList[index].atta == undefined) {
                        this.formData.recordfiles.push({
                            fId: this.fileList[index].fId,
                        });
                    } else {
                        this.formData.recordfiles.push({
                            fId: this.fileList[index].atta.fId,
                        });
                    }
                    this.fileList.splice(index, 1);
                }
            }
        },
        // 详情
        getDetail(obj) {
            this.loading = true;
            getInstrumentInfo(obj).then((res) => {
                if (res.errcode == 0) {
                    var data = res.data.yjQmpMsInstrumentinfo;
                    this.formData = data
                    this.loading = false;
                    this.fileList = res.data.fFilelist;
                    this.formData.recordfiles = [];
                    // var selected = []
                    // if (data.fVarietyid != '') {
                    //     var id = data.fVarietyid.split("、")
                    //     var name = data.fVarietyname.split("、")
                    //     id.map((item, index) => {
                    //         selected.push({
                    //             label: name[index],
                    //             value: item
                    //         })
                    //     })
                    // } else {
                    //     selected = []
                    // }
                    // this.selected = [{
                    //     name: "fVarietyname1",
                    //     selected: selected
                    // }]
                    if (this.statusNum == 7) {
                        this.btnShow = false;
                    } else {
                        this.btnShow = true;
                    }
                } else {
                    this.loading = false;
                    this.$message.error(res.msg);
                }
            });
        },
        yj_saveForm(status) {
            // this.formData.userId = this.userInfo.userId; //当前登录人id
            // this.formData.userName = this.userInfo.realName; //当前登录人名称
            // var name = [];
            // var id = [];
            // this.formData.fVarietyname1.map(item => {
            //     name.push(item.label)
            //     id.push(item.value)
            // })
            // this.formData.fVarietyid = id.join("、")
            // this.formData.fVarietyname = name.join("、")
            // 0连续新增1新增
            // this.formData.companyId=this.fCompanyid;
            if (this.$refs.publicForm.submitForm()) {
                this.saveForm(this.formData, status);
            } else {
                console.log("error");
            }
        },
        // 保存
        saveForm(data, status) {
            this.loading = true;
            // this.formData.typ = this.typs
            this.isLock = true
            saveInstrumentInfo(data)
                .then((res) => {
                    if (res.errcode == 0) {
                        this.loading = false;
                        this.visible = false;
                        this.$emit("refreshList", status);
                        this.$message.success("新增成功");
                        this.isLock = false
                    } else if (res.errcode == -1) {
                        this.loading = false;

                        this.visible = false;
                        this.$message.error(res.data);
                        this.$emit("refreshList", status);
                        this.isLock = false
                    } else {
                        this.loading = false;
                        this.isLock = false
                        this.$message.error(res.data);
                    }
                })
        },
    },
};